const Question = require('../modules/question');
const AnswerChoice = require('../modules/answerchoice');
const AnswerLog = require('../modules/answerlog');

class QuestionController{

    // static async index(ctx) {
    //     try {
    //         const data = {}
    //         data.learnContentList = await new LearningContent().listALl()
    //         await ctx.render('teachmaterial/index', data)
    //     } catch(err) {
    //         await ctx.render('error', {message: err.message})
    //     }
    // }

    static async onePage(ctx) {
        try {
            const id = ctx.params.id
            let data = {}
            let q = await new Question()
            let res = await q.getOne(id)
            data = res
            await ctx.render('question/onepage', data)
        } catch(err) {
            await ctx.render('error', {message: err.message})
        }
    }

    static async onePageDetail(ctx) {
        try {
            const teachmaterialid = ctx.params.teachmaterialid
            let data = {}
            let q = await new Question()
            let res = await q.getTenQuestionByteachMaterialId(teachmaterialid)
            let arr=[];
            let json={};
            let sumScore = 0;
            while(arr.length<10){
                var k=Math.round(Math.random()*(res.length-1));
                if(!json[k]){
                json[k]=true;
                let tmp = res[k]
                let al = await new AnswerChoice()
                let answerChoice = await al.listAnswerChoinceByQuestionId(tmp.id)
                tmp.answerChoice = answerChoice
                arr.push(tmp);
                sumScore += tmp.score;
                }
            }
            data.questionList = arr
            data.sumScore = sumScore
            await ctx.render('question/onepageDetail', data)
        } catch(err) {
            await ctx.render('error', {message: err.message})
        }
    }

    static async calculateScore(ctx) {
        try {
            const body = ctx.request.body;
            let finalScore = 0
            let sum = 0
            for(let key in body){
                if (key != 'sumScore') {
                    let q = await new Question()
                    let res = await q.getOne(key)
                    finalScore += (res.score * body[key])
                } else {
                    sum = body[key]
                }
            }
            let data = {}
            data.finalScore = finalScore
            data.finalRate = parseInt((finalScore / sum) * 100)
            data.isSuccess = (finalScore / sum) > 0.4
            let al = await new AnswerLog()
            await al.add(ctx.session.currentUserId, finalScore, data.isSuccess ? 1 : 0);
            await ctx.render('question/yourAnswerScore', data)
        } catch(err) {
            await ctx.render('error', {message: err.message})
        } 
    }

    static async detail(ctx) {
        try {
            const id = ctx.params.id
            let data = {}
            let q = await new Question()
            let res = await q.getOne(id)
            data = res
            await ctx.render('question/detail', data)
        } catch(err) {
            await ctx.render('error', {message: err.message})
        }
    }
    
}

exports = module.exports = QuestionController;